#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFile>
#include<QTextStream>
#include<QDebug>
#include"src/slidenavigation.h"
#include<QMenu>
#include<QAction>
#include"src/login.h"
#include<QDialog>
#include<QApplication>
#include"src/mes.h"
#include<QStackedWidget>

#include"src/page0.h"   //我的设备
#include"src/page2.h"   //点检看板
#include"src/importwidget.h"    //设备导入
#include"src/page1.h"   //设备领用
#include"src/page3.h"   //设备查询
#include"src/repairwidget.h"    //设备维修
#include"src/page4.h"   //设备报废
#include"src/verwidget.h"   //设备校验  取消
#include"src/adminwidget.h" //后台管理


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    pMenu=new QMenu(this);
    changeAccount=new QAction("切换账号",this);
    quitPAction=new QAction("退出",this);
    pMenu->addAction(changeAccount);
    pMenu->addAction(quitPAction);
    ui->toolButton->setPopupMode(QToolButton::InstantPopup);
    ui->toolButton->setMenu(pMenu);

    systemTray = new QSystemTrayIcon(this);
    systemTray->setIcon(QIcon(":/logo.ico"));
//    systemTray->setToolTip(QString("宝德生产设备管理：%1").arg(Mes::getInstance()->name));
    myMenu=new QMenu(this);
    restoreWinAction=new QAction("显示主界面",this);
    hideAction=new QAction("隐藏至托盘",this);
    changeAction=new QAction("切换账号",this);
    quitAction=new QAction("退出",this);
    myMenu->addAction(restoreWinAction);
    myMenu->addAction(hideAction);
    myMenu->addAction(changeAction);
    myMenu->addAction(quitAction);
    systemTray->setContextMenu(myMenu);

    this->initNav();

    connect(systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
    connect(restoreWinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
    connect(hideAction, SIGNAL(triggered()), this, SLOT(hide()));
    connect(changeAction, SIGNAL(triggered()), this, SLOT(changeToLogin()));
    connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));

    connect(changeAccount,SIGNAL(triggered()),this,SLOT(changeToLogin()));
    connect(quitPAction,SIGNAL(triggered()),this,SLOT(close()));
//    tChangeFirst->setInterval(200);
//    tChangeFirst->start();
//    connect(tChangeFirst,SIGNAL(timeout()),ui->widget,SLOT(moveToFirst()));
}

void MainWindow::changeToLogin()
{
    systemTray->hide();
    this->close();
    Login l(firstLogin);
    if(l.exec()==QDialog::Accepted){          // 从登陆窗口访问数据库获得角色账号，姓名，权限
        this->show();
    }else{
        this->close();
    }
    firstLogin=false;

    for(int i=ui->stackedWidget->count();i>=0;i--){
        QWidget *widget=ui->stackedWidget->widget(i);
        ui->stackedWidget->removeWidget(widget);
        widget->deleteLater();
    }

    ui->widget->deleteLater();
    this->initNav();
}

int MainWindow::getRole()
{
    int binRole=WORDER;        // 默认权限
    for(QString s:Mes::getInstance()->role){
        if(s.contains("管理员")){
            binRole |= ADMIN;continue;
        }else if (s.contains("经理")) {
            binRole |= MANAGER;continue;
        }else if (s.contains("组长")){
            binRole |= GROUPER;continue;
        }else{
            binRole |= WORDER;continue;
        }
    }

    if(binRole>MANAGER){
        if(binRole>ADMIN){
            return ADMIN;
        }else{
            return MANAGER;
        }
    }else{
        if(binRole>GROUPER){
            return GROUPER;
        }else{
            return WORDER;
        }
    }
}

void MainWindow::initNav()
{
    ui->widget=new SlideNavigation;
    ui->widget->setFixed(true);
    ui->widget->setOrientation(Qt::Vertical);
    ui->verticalLayout->insertWidget(0,ui->widget);
    constRole=getRole();
    if(constRole==ADMIN){
        ui->widget->addItem("我的设备");
        ui->widget->addItem("点检看板");
        ui->widget->addItem("设备领用");
        ui->widget->addItem("设备查询");
        ui->widget->addItem("设备维修");
        ui->widget->addItem("设备报废");
        ui->widget->addItem("设备导入");
        ui->widget->addItem("后台管理");
    }else if(constRole==MANAGER){
        ui->widget->addItem("我的设备");
        ui->widget->addItem("点检看板");
        ui->widget->addItem("设备领用");
        ui->widget->addItem("设备查询");
        ui->widget->addItem("设备维修");
        ui->widget->addItem("设备报废");
        ui->widget->addItem("设备导入");
    }else if(constRole==GROUPER){
        ui->widget->addItem("我的设备");
        ui->widget->addItem("点检看板");
        ui->widget->addItem("设备领用");
        ui->widget->addItem("设备查询");
        ui->widget->addItem("设备维修");
        ui->widget->addItem("设备报废");
    }else{
        ui->widget->addItem("我的设备");
        ui->widget->addItem("点检看板");
//        ui->widget->moveToFirst();
    }

    systemTray->setToolTip(QString("宝德生产设备管理：%1").arg(Mes::getInstance()->name));
    systemTray->show();

    connect(ui->widget,SIGNAL(itemClicked(int,QString)),this,SLOT(changeWidget(int,QString)));  // 切换功能窗口
    changeWidget(0,"firstLogin");
}


void MainWindow::changeWidget(int index,QString s)
{
    Q_UNUSED(s);
//    ui->stackedWidget->setCurrentIndex(index);
    switch(index) {
        case 0:widgetList[index]=new Page0;break;
        case 1:widgetList[index]=new Page2;break;
        case 2:widgetList[index]=new Page1;break;
        case 3:widgetList[index]=new Page3(constRole);break;
        case 4:widgetList[index]=new RepairWidget;break;
        case 5:widgetList[index]=new Page4;break;
        case 6:widgetList[index]=new ImportWidget;break;
        case 7:widgetList[index]=new AdminWidget;break;
    }
//    qDebug()<<index;
    ui->stackedWidget->insertWidget(index,widgetList[index]);
    ui->stackedWidget->setCurrentWidget(widgetList[index]);
}

MainWindow::~MainWindow()
{
    Mes::getInstance()->release();
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    QAction *action=(QAction *)sender();
    if(action==nullptr){
        event->ignore();
        this->hide();
        systemTray->showMessage("设备管理系统","退出到系统托盘");
    }else{
        event->accept();
    }
}

void MainWindow::iconIsActived(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason)
    {
    //点击托盘显示窗口
    case QSystemTrayIcon::Trigger:
     {
       showNormal();
       break;
     }
    //双击托盘显示窗口
    case QSystemTrayIcon::DoubleClick:
    {
      showNormal();
      break;
    }
    default:
     break;
    }
}

//void MainWindow::initStyle()
//{
//    //加载样式表
//    QString qss;
//    //QFile file(":/qss/psblack.css");
//    //QFile file(":/qss/flatwhite.css");
//    QFile file(":/lightblue.css");
////    QFile file(":/psblack.css");
//    if (file.open(QFile::ReadOnly)) {
//#if 1
//        //用QTextStream读取样式文件不用区分文件编码 带bom也行
//        QStringList list;
//        QTextStream in(&file);
//        //in.setCodec("utf-8");
//        while (!in.atEnd()) {
//            QString line;
//            in >> line;
//            list << line;
//        }

//        qss = list.join("\n");
//#else
//        //用readAll读取默认支持的是ANSI格式,如果不小心用creator打开编辑过了很可能打不开
//        qss = QLatin1String(file.readAll());
//#endif
////        qDebug()<<qss;
//        QString paletteColor = qss.mid(20, 7);
//        qApp->setPalette(QPalette(QColor(paletteColor)));
//        qApp->setStyleSheet(qss);
//        file.close();
//    }
//}

//void MainWindow::enterEvent(QEvent *)
//{
//    setStyleSheet("QToolButton{background-color:rgb(46,132,243)}");
//}
//void MainWindow::leaveEvent(QEvent *)
//{
//    setStyleSheet("QToolButton{background-color:rgb(80,80,80)}");
//}
